import { DomUtils } from "../utils/DomUtils";

/**
 * 简书 (https://www.jianshu.com) 页面美化
 */
export class JianShu {

    static hostname = 'www.jianshu.com';

    main() {
        DomUtils.hide([
            // 顶部header
            'header',
            // 左侧 点赞、赞赏、手机看全文等按钮
            '._3Pnjry',
            // 右侧侧边栏
            '._2OwGUo',
            // 底部footer，包含输入框、收藏按钮
            'footer',
            ...(document.querySelectorAll('article._2rhmJa ~ div')),
            // 图片广告
            ...(document.querySelectorAll('.adad_container'))
        ]);

        // 隐藏文档底部的二维码、作者信息
        DomUtils.observe(document.querySelectorAll('section.ouvJEz')[0], ['childList'], { childList: true }, 
            ({ observer, index, target }) => {
            const _1kCBjS = target.querySelector('._1kCBjS');
            const _13lIbp = target.querySelector('._13lIbp');
            const d0hShY = target.querySelector('.d0hShY');
            if (_1kCBjS) {
                _1kCBjS.style.display = 'none';
                index++;
            }
            if (_13lIbp) {
                _13lIbp.style.display = 'none';
                index++;
            }
            if (d0hShY) {
                d0hShY.style.display = 'none';
                index++;
            }
            if (index === 3) {
                observer.disconnect();
            }
        });
        // 文章下面的广告区域
        DomUtils.hide(document.querySelectorAll('section.ouvJEz')[1]);
        
        // 评论区域
        DomUtils.observe('#note-page-comment', ['childList'], { childList: true }, ({ target }) => {
            DomUtils.handleComment(target, '._2R7vBo', '._26JdYM', '0', () => observer.disconnect());
        });

        // 页面底部的推荐阅读区域
        DomUtils.hide(document.querySelectorAll('section.ouvJEz')[2]);

        // 处理登录弹窗
        DomUtils.observe('body', ['childList'], { childList: true }, ({ observer }) => {
            const loginModal = document.querySelector('._23ISFX-wrap._23ISFX-wrap-middle');
            if (loginModal) {
                DomUtils.hide(loginModal.parentElement);
                document.body.style.overflow = 'auto';
                observer.disconnect();
            }
        });

        // 处理文章样式，包括宽度、边距、圆角
        DomUtils.setCss('#__next', { 
            width: '900px', 
            margin: '20px auto 0', 
            paddingBottom: '20px', 
            borderRadius: '10px',
            overflow: 'hidden'
        });
        DomUtils.setCss('._21bLU4._3kbg6I', { borderRadius: '10px' })
        DomUtils.setCss('._gp-ck', { width: '900px', margin: 0 });
        DomUtils.setCss('._3VRLsv', { width: '900px', padding: 0 });
        DomUtils.setCss(document.querySelectorAll('.ouvJEz')[0], { paddingTop: 0, paddingBottom: 0 });

        DomUtils.setBackground();
    }
}